function setActive(activate){

	if(activate){
		PanelSizeController.init();
		localisation.init(); // apply the local language
		Transition.init(); // starts the animation
	}else{
		$('#mia-container').cycle("stop"); 
	}
	return 1
}

var PanelSizeController = {
	
	LOBBYMODE_MINI: 5,
		
	init: function() {
		
		if (system.get_lobby_mode() == PanelSizeController.LOBBYMODE_MINI) {
			
			$('#container').addClass('small');
		}
	}
}

// Panel Specific Javascript
var Transition = {

	first_timeout:300000,

	init:function() {

			Transition.first_timeout = 10000;
/*		}
*/
	    $('#mia-container').show().cycle({ 
		    fx:    'cover', 
		    speed: 300, 
		    timeoutFn: calcPanelTimeOut,
			timeout:Transition.first_timeout,
		  	pause: false,
			before:  onBefore, 
			after:   onAfter,
			startingSlide: 0
		});


		$('#mia-container').hover(
		function(){ // On hover
			$('#mia-container').cycle("pause");
		},
		function(){ // On hover out 
			if(slideControl.state() != "busy"){
				$('#mia-container').cycle("resume");
			}
		});


		// Get the timeout values from the attributes
		function calcPanelTimeOut(currElement, nextElement, opts, isForward) { 

    		var json = {"panels":[{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000},{"timeout":10000}]}

    		if(typeof(currElement.id) != "undefined"){
    			var time = json.panels[currElement.id]["timeout"];
    		}
    		return time; 
		} 

        // Calbacks to the Client
       function onBefore(curr, next, opts, fwd){

           if(slideControl.state() == "idle" && curr.id != 0){
               system.transition_start();
           }
       }
       // Callbacks to the Client
       function onAfter(curr, next, opts, fwd){

           if(slideControl.state() == "idle"  && curr.id != 0){
               system.transition_end();
           }
       }		
	}
}        


var system = {
	
	Event: {RESPONSE_RECEIVED: 'response'},
	SERVICE_URL: 'http://mia.fulltiltpoker.' + BRAND + '/video-preference',
	REQUEST_TIMEOUT: 8000,
	POLLING_FREQUENCY: 1000,

	get_lobby_mode: function() {
		if(typeof miawidget != 'undefined'){
			if(typeof miawidget.getLobbyMode == "function"){
				return miawidget.getLobbyMode();
			}
		}
		
		return null;
	},

	transition_start:function(){
		if(typeof miawidget != 'undefined'){
			if(typeof miawidget.onTransitionStart == "function"){
				miawidget.onTransitionStart();
			}
		}
	},

	transition_end:function(){
		if(typeof miawidget != 'undefined'){
			if(typeof miawidget.onTransitionEnd == "function"){
				miawidget.onTransitionEnd();
			}
		}
	},
	
	trigger_event: function() {

		$(system).trigger(system.Event.RESPONSE_RECEIVED);

	},
	
	get_secure_url_key: function() {

		if (typeof miawidget != 'undefined' && typeof miawidget.getSecureURLKey == 'function') {
			return miawidget.getSecureURLKey();
		}
		return null;
	},

	add_token_to_url:function(url){

		var token = system.get_secure_url_key();

		if(!token){
			return url;
		}

		var query_string = "";
		var tempArray = url.split("?");
		var base = tempArray[0];
		var aditionalURL = tempArray[1]; 
		var temp = "";
		if(aditionalURL){
			var tempArray = aditionalURL.split("&");
			for ( var i in tempArray ){
		        if(tempArray[i].indexOf("key") == -1){
		        	query_string += temp+tempArray[i];
		            temp = "&";
		        }
		    }
		}
		var qs = temp + "key=" + token;
		var finalURL = base+"?"+query_string + qs;

		return finalURL;
	}
};

var slideControl = {

    transition_state: 'idle',

	init: function(){
	 // constructables
	},

	state: function(){

	    return slideControl.transition_state;    
	},

	set_state: function(state) {

	    slideControl.transition_state = (!state) ? 'idle' : 'busy';	    
	}
};

var Tracking = {
	Event: {START: 'start', COMPLETED: 'complete'},
    TRACKING_URL: 'http://w.fulltiltpoker.' + BRAND + '/mia-tracking',

  	track: function(media, description, status) {

        var parameters = {
            brand: BRAND,
            media: media,
            name: description,
			status: status || Tracking.Event.START
        };

        var address = Tracking.TRACKING_URL + '?';

        var query_string = [];

        for(var i in parameters) {
            query_string.push(i + '=' + encodeURIComponent(parameters[i]));
        }

        var img = new Image();
        img.src = address + query_string.join('&');  
    },

	click_handler: function(element, tracking_description, asset_id) {

		element.href += localisation.get_asset(asset_id) + '&player_id=' + localisation.get_asset('player') + '&offer_ix=' + localisation.get_asset(asset_id + '_offer_ix');

		href = system.add_token_to_url(element.href);
		
		element.href = href;

		$(element).mouseout(); 		
	}
}

//Plugin for disabling selection
$.fn.disableSelection = function() {
    $(this).attr('unselectable', 'on')
           .css({'-moz-user-select':'none','-webkit-user-select':'none'})
           .each(function() { 
               this.onselectstart = function() { return false; };
            });
};


$(document).ready(function (){
	$("div.panel").disableSelection(); 
});